"use strict";
///////////////////历史数据///////////////////
const bag = 94;
const mvc = require('cosjs.mvc');
const dataset = require("cosjs.updater").dataset;
const ObjectID = require("../../model/ObjectID");
class collect extends dataset.table {
    constructor(updater) {
        super(updater, bag, 'collect');
        this.sort = bag;
        this._Math_Act = [];  //max min
    }
    types(id) {
        let ret, str = String(id);
        if (str.indexOf("-") < 0) {
            ret = { "id": str, "_id": ObjectID(this.updater.uid, id) };
        }
        else {
            let arr = str.split("-");
            ret = { "id": Number(arr[1]), "_id": str };
        }
        return ret;
    }

    format(item, act) {
        let itemBag = this.updater.iTypes(item.id);
        if (itemBag.bag == 20) {
            item['type'] = 1;
        } else if (itemBag.bag == 30) {
            item['type'] = 2;
        } else {
            item['type'] = 3;
        }
        return mvc.format("collect", item, true);
    }

    get(id) {
        let d = super.get(id) || {};
        return d["val"] || 0;
    }
    //插入一个最大值
    max(id, val, type) {
        let ty = this.key(id);
        ty['v'] = val;
        ty['t'] = 1;
        // ty['type'] = type;
        this._Math_Act.push(ty);
    }
    //插入一个最小值
    min(id, val) {
        let ty = this.key(id);
        ty['v'] = val;
        ty['t'] = -1;
        this._Math_Act.push(ty);
    }
    del() {
        return null
    };
    sub() {
        return null
    };
    // id,val,type
    add() {
        //t,id,k,v,bag
        return this.update(this.model.ObjectID(this.model.uid, arguments[0]))
        return super.act('insert', { '_id': this.model.ObjectID(this.model.uid, arguments[0]), "id": arguments[0], "type": arguments[2] }, "val", arguments[1], bag)

    };

    set(id, val, type) {
        return this.act('set', id, { "val": val, "time": Date.now() })
        // return this.act('set',this.model.ObjectID(this.model.uid,arguments[0]),{'_id':this.model.ObjectID(this.model.uid,arguments[0]),"id":arguments[0],"type":arguments[2],"val":10},bag);
        // return  this.act('set',{'_id':this.model.ObjectID(this.model.uid,arguments[0]),"id":arguments[0],"type":arguments[2]},"val",arguments[1],bag)
    };

    verify() {
        //预处理 min，max
        for (let d of this._Math_Act) {
            let v = this.get(d) || 0;
            if ((d['t'] > 0 && d['v'] > v) || (d['t'] < 0 && d['v'] < v)) {
                this.set(d.id, d['v'], d['type']);
            }
        }
        //进入正常流程
        return super.verify();
    }

};

module.exports = collect;